package com.laowang.project.pay.config;

import com.github.wxpay.sdk.WXPayConfig;
import lombok.Data;
import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Auther laowang
 * @Date 2021/6/10
 * @Description  退款有用这个类
 *   微信小程序配置
 */


@Data
public class WxPayConfigUtil implements WXPayConfig {

    private byte[] certData;

    private String APP_ID;   // wxbd9aa9a0b6342bcc
    private String MCH_ID;   //152481968
    private String API_KEY;  //MIID9TCCAt2gAwIBAgIUZkKGumBVFuHS


    @Override
    public String getAppID() {
        return APP_ID;
    }

    @Override
    public String getMchID() {
        return MCH_ID;
    }


    @Override
    public String getKey() {
        return API_KEY;// 签名key
    }

    @Override
    public InputStream getCertStream() {
        return new ByteArrayInputStream(certData);
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 0;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 0;
    }


    public WxPayConfigUtil(String APP_ID,String MCH_ID,String API_KEY,String fileName)  {
        this.APP_ID = APP_ID;
        this.MCH_ID = MCH_ID;
        this.API_KEY = API_KEY;
        try {
            ClassPathResource classPathResource = new ClassPathResource("/p12/apiclient_cert"+fileName+".p12");
            InputStream certStream = classPathResource.getInputStream();
            this.certData = IOUtils.toByteArray(certStream);
            certStream.read(this.certData);
            certStream.close();
        } catch (IOException var3) {
            var3.printStackTrace();
        }

    }

}
